package com.bjb.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
	/**
	 * 日期格式转换为时间戳
	 *
	 * @param dateStr
	 * @param pattern
	 * @return
	 */
	public static String dateTimestamp(String dateStr, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		format.setLenient(false);
		String timestamp = "";
		try {
			timestamp = (format.parse(dateStr).getTime() / 1000) + "";
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return timestamp;
	}

	/**
	 * 时间戳转换为日期格式
	 *
	 * @param timestamp
	 * @param pattern
	 * @return
	 */
	public static String timestampDate(String timestamp, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		String dateStr = "";
		dateStr = format.format(new Date(Long.valueOf(timestamp)));
		return dateStr;
	}

	/**
	 * 时间戳转换为日期格式
	 *
	 * @param timestamp
	 * @return
	 */
	public static String timestampDate(String timestamp) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = "";
		dateStr = format.format(new Date(Long.valueOf(timestamp)));
		return dateStr;
	}

	/**
	 * 将一个Date格式的日期转换为自定义格式的日期
	 *
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String dateFormat(Date date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		String dateStr = format.format(date);
		return dateStr;
	}

	/**
	 * 将一个Date格式的日期转换为自定义格式的日期
	 *
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date dateToOtherDate(Date date, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		format.setLenient(false);
		String dateStr = format.format(date);
		Date parseDate = null;
		try {
			parseDate = format.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return parseDate;
	}

	/**
	 * 获取两个日期之间的天数
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int getDaysBetweenDates(Date startDate, Date endDate) {
		startDate = dateToOtherDate(startDate, "yyyy-MM-dd");
		endDate = dateToOtherDate(endDate, "yyyy-MM-dd");
		long days = (endDate.getTime() - startDate.getTime()) / (60 * 60 * 24 * 1000);
		return Integer.valueOf(String.valueOf(days));
	}

	/**
	 * 判断时间格式 格式必须为“YYYY-MM-dd” 2004-2-30 是无效的 2003-2-29 是无效的
	 *
	 * @param sDate
	 * @return
	 */
	public static boolean isLegalDate(String sDate) {
		int legalLen = 10;
		if ((sDate == null) || (sDate.length() != legalLen)) {
			return false;
		}

		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date date = formatter.parse(sDate);
			return sDate.equals(formatter.format(date));
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * string转date
	 * @param str
	 * @return
	 * @throws ParseException
	 */
	public static Date strToDate(String str) throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
		Date date = formatter.parse(str);
		return date;
	}

	public static Date strToDate2(String str) throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy/MM/dd HH:mm");
		Date date = formatter.parse(str);
		return date;
	}

	/**
	 * 获取当前日期（yyyy-MM-dd）格式
	 * @return
	 */
	public static String getCurrentDate(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String currentDateStr = format.format(new Date()).toString();
		return currentDateStr;
	}

	public static void main(String args[]) throws ParseException {
		System.out.println("timestampDate = " + timestampDate("1561860000" + "000"));
	}

}
